<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\MessageTemplate;
use App\Admin\Utils\Utils;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Layout\Content;

class MessageTemplateController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new MessageTemplate(), function (Grid $grid) {
            $grid->disableEditButton();

            $grid->column('template_id', '模板 ID');
            $grid->column('content');
            $grid->column('type')->using([0 => '验证码', 1 => '通知'])->badge([0 => 'yellow', 1 => 'blue']);
            $grid->column('status')
                ->using([0 => '驳回', 1 => '报备中', 2 => '报备通过'])
                ->dot([0 => 'red', 1 => 'blue', 2 => 'green']);
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

                $filter->scope('trashed', '回收站')->onlyTranshed();
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new MessageTemplate(), function (Show $show) {
            $show->panel()->tools(function ($tools) {
                $tools->disableEdit();
            });

            $show->field('id');
            $show->field('template_id');
            $show->field('content');
            $show->field('type')->using([0 => '验证码', 1 => '通知'])->badge('yellow');
            $show->field('status')
                ->using([0 => '驳回', 1 => '报备中', 2 => '报备通过'])
                ->dot([0 => 'red', 1 => 'blue', 2 => 'green']);
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new MessageTemplate(), function (Form $form) {
            $form->disableEditingCheck();

            $form->display('id');
            $form->radio('addType', '添加方式')->options([0 => '同步平台模板', 1 => '新建模板'])->default(1)
                ->when(0, function (Form $form) {
                    $form->text('template_id', '模板 ID');
                });
            $form->textarea('content')->required();
            $form->radio('type')->options([0 => '验证码', 1 => '通知'])->default(1);
            $form->hidden('status');
            $form->display('created_at');
            $form->display('updated_at');

            $form->submitted(function (Form $form) {
                $method = $form->addType;
                $form->deleteInput('addType');

                if ($method == 0) {
                    $form->status = 2;
                    return true;
                } else {
                    $util = new Utils();
                    $reuslt = $util->addTemplate($form->content);
                    if ($reuslt['code'] == 2) {
                        $form->template_id = $reuslt['templateid'];
                        $form->status = 1;
                        return true;
                    } else {
                        return $form->response()->error($reuslt['msg']);
                    }
                }
            });
        });
    }
}
